package com.cslg.ac.demo.controller.auth.mail;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.email.EmailAddress;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.service.auth.mail.MailService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@Controller
public class MailController {
    @Autowired
    MailService mailService;


    @RequestMapping(value = "/email/isAuth",method = RequestMethod.POST)
    public void isAuth(@RequestParam int id,PrintWriter out){
        int code;
        try {
            code = mailService.isAuth(id);
        }catch (Exception e){
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }

    /**
     * 可以ong le  y
     * 发送验证邮件(终于...链接可以按了...)
     * @param email
     * @param out
     * @param session
     * @return
     */

    @RequestMapping(value = "/user/authEmail",method = RequestMethod.POST)
    public void authMail(@RequestParam("email") String email
    ,PrintWriter out,HttpSession session){
        User user = (User) session.getAttribute("user");
        String id = user.getUserName();
        StringBuffer sb = new StringBuffer("点击下面链接激活账号，1小时生效，否则账号不能登陆，链接只能使用一次，请尽快激活！</br>");
        sb.append("<a href=\"http://111.230.116.176:9800/user/authSucceed/")
//        sb.append("<a href=\"http://localhost:9800/user/authSucceed/")
                .append(email)
                .append("/")
                .append(id)
                .append("\">http://111.230.116.176:9800/user/authSucceed/")
//                .append("\">http://localhost:9800/user/authSucceed/")
                .append(email)
                .append("/")
                .append(id)
                .append("</a>");

        EmailAddress emailAddress = new EmailAddress("亲爱的lovingpet用户",email);
        int code = ResponseCode.SERVER_ERROR;
        try {
             code = mailService.authEmail(sb.toString(),emailAddress);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,code);
        }

        ResponseUtil.responseCode(out,code);

    }

    /**
     * 之前一直出现 sessionId到这突变，原因是：
     * 登陆和发送邮件用的postman，唯独激活用的浏览器。。。sessionId就变了。。。
     * @param email
     * @param out
     */
    @RequestMapping(value = "/user/authSucceed/{email}/{id}",method = RequestMethod.GET)
    public void authSucceed(@PathVariable String email,@PathVariable String id,
                            PrintWriter out){
        try {
            int code =  mailService.authSucceed(email,id);
        } catch (Exception e) {
            e.printStackTrace();
            out.println("Sorry, because of the system error, please return to the activation place to get the mail again!");
            return;
        }
        out.println("Congratulations, it's a success!");


    }



}
